/***
This creates event study plots, estimating the stimulus effects.
***/

*-------------------------------------------------------------------------------
* Setup
*-------------------------------------------------------------------------------

* Set $root
project figstabs, root
if (r(buildrunning)==0) include "${root}/code/config_interactive.do"

* Set globals
project, uses("${root}/code/set_globals.do")
include "${root}/code/set_globals.do"

* Create required subfolders
cap mkdir "${root}/results/Policy"

* Set seed
set seed 1280

*-------------------------------------------------------------------------------
**# 1. Load and process data
*-------------------------------------------------------------------------------

* Load dataset
project, uses("${root}/data/derived/Policy/stimulus_did_dataset.dta")
use "${root}/data/derived/Policy/stimulus_did_dataset.dta", clear
gisid round income_q date

* We're going to plot the excluded dates for transparency, but will continue to omit them from regressions
gen excluded_dates = inlist(date, mdy(4, 14, 2020), mdy(4, 14, 2019)) if round == "april"
replace excluded_dates = inrange(date, mdy(3, 13, 2021), mdy(3, 16, 2021)) | inrange(date, mdy(3, 13, 2019), mdy(3, 16, 2019)) if round == "march"
replace analysis_window = 1 if excluded_dates == 1
assert missing(post) if excluded_dates == 1

* First difference: post-Covid vs. pre-Covid
foreach spec in "" "_notrend" {

	* Normed spending residualized on DoW FEs in the control year
	gen temp = dow_resid_2019`spec' if treat == 0 & analysis_window == 1
	egen dow_resid_control`spec' = mean(temp) if analysis_window == 1, by(round income_q month day)
	assert temp == dow_resid_control`spec' if treat == 0 & analysis_window == 1
	drop temp

	* First difference
	gen first_diff`spec' = dow_resid_2019`spec' - dow_resid_control`spec'
	assert first_diff`spec' == 0 if treat == 0 & analysis_window == 1
	assert !missing(first_diff`spec') if analysis_window == 1
	assert missing(first_diff`spec') if analysis_window == 0
}

* We no longer need the pre-Covid year - drop accordingly
drop if treat == 0

* For presentation, recenter such that the first difference is zero in the pre-period (excluding the excluded dates when recentering)
foreach spec in "" "_notrend" {

	gen temp = first_diff`spec' if post == 0
	assert missing(temp) if excluded_dates == 1
	egen first_diff`spec'_pre = mean(temp), by(round income_q)
	drop temp

	gen first_diff`spec'_recenter = first_diff`spec' - first_diff`spec'_pre
	assert !missing(first_diff`spec'_recenter) if analysis_window == 1
	assert missing(first_diff`spec'_recenter) if analysis_window == 0
}

* Convert variables to percentages
replace first_diff_recenter = first_diff_recenter * 100
replace first_diff_notrend_recenter = first_diff_notrend_recenter * 100

*------------------------------------------------------------------------------*
* Checks
*-------------------------------------------------------------------------------

* Assert equality of first difference in detrended value and detrended first differences, up to rounding error
gen notrend_first_diff_recenter = .

foreach round in april march {
	reg first_diff_recenter date if post == 0 & round == "`round'"

	* 24 days pre- for April; 21 days pre- for March
	if "`round'" == "april" assert e(N) == 24 * 4
	else if "`round'" == "march" assert e(N) == 21 * 4

	predict detrended, resid
	replace notrend_first_diff_recenter = detrended if round == "`round'"
	drop detrended
}

assert inrange(first_diff_notrend_recenter, notrend_first_diff_recenter - 1E-5, notrend_first_diff_recenter + 1E-5) if analysis_window == 1
drop notrend_first_diff_recenter


* Assert that mean recentered first differences equal the diff-in-diff estimates, up to rounding error
project, uses("${root}/data/derived/stimulus_estimates.dta")

foreach round in april january march {
	forval income_q = 1/4 {
		preserve

		sum first_diff_notrend_recenter if first_five == 1 & round == "`round'" & income_q == `income_q'
		loc mean_notrend_first_five = r(mean)
		sum first_diff_recenter if first_five == 1 & round == "`round'" & income_q == `income_q'
		loc mean_first_five = r(mean)

		sum first_diff_notrend_recenter if after_first_five == 1 & round == "`round'" & income_q == `income_q'
		loc mean_notrend_after_first_five = r(mean)
		sum first_diff_recenter if after_first_five == 1 & round == "`round'" & income_q == `income_q'
		loc mean_after_first_five = r(mean)

		use "${root}/data/derived/stimulus_estimates.dta", clear

		sum first_five_pp if round == "`round'" & income_q == `income_q' & estimate == "beta" & specification == "detrended"
		assert inrange(r(mean), `mean_notrend_first_five' - 1E-5, `mean_notrend_first_five' + 1E-5)

		sum first_five_pp if round == "`round'" & income_q == `income_q' & estimate == "beta" & specification == "original"
		assert inrange(r(mean), `mean_first_five' - 1E-5, `mean_first_five' + 1E-5)

		sum after_first_five_pp if round == "`round'" & income_q == `income_q' & estimate == "beta" & specification == "detrended"
		assert inrange(r(mean), `mean_notrend_after_first_five' - 1E-5, `mean_notrend_after_first_five' + 1E-5)

		sum after_first_five_pp if round == "`round'" & income_q == `income_q' & estimate == "beta" & specification == "original"
		assert inrange(r(mean), `mean_after_first_five' - 1E-5, `mean_after_first_five' + 1E-5)

		restore
	}
}

*-------------------------------------------------------------------------------
**# 2. Scatter plots
*-------------------------------------------------------------------------------

foreach depvar in first_diff_notrend_recenter first_diff_recenter {

	if "`depvar'" == "first_diff_notrend_recenter" local graph_title ""
	else if "`depvar'" == "first_diff_recenter" local graph_title " original"

	* April, Q1
	preserve

	keep if round == "april" & income_q == 1 & analysis_window == 1
	gisid date

	tw ///
	(scatter `depvar' date, mcolor(oi1)) ///
	, ///
	xlab(`=mdy(3, 18, 2020)' `""Mar 18" "2020""' `=mdy(3, 25, 2020)' "Mar 25" ///
		 `=mdy(4, 1, 2020)' "Apr 1" `=mdy(4, 8, 2020)' "Apr 8" `=mdy(4, 15, 2020)' "Apr 15" ///
		 `=mdy(4, 22, 2020)' "Apr 22" `=mdy(4, 29, 2020)' "Apr 29" `=mdy(5, 6, 2020)' "May 6" ) ///
	xline(`=mdy(4,14,2020)', lcolor(gs8) lpattern(dash)) ///
	xline(`=mdy(4,15,2020)', lcolor(gs8)) ///
	text(57 `=mdy(4,14,2020) - 1' "Apr" "14", color(gs8) size(medsmall))  ///
	text(57 `=mdy(4,15,2020) + 1' "Apr" "15", color(gs8) size(medsmall)) ///
	title(" ", size(tiny)) ///
	yline(0, lcolor(gs8) lpattern(dash)) ///
	xtitle("") ///
	ylab(-20 "-20%" 0 "0%" 20 "+20%" 40 "+40%", nogrid) ///
	yscale(range(-20 50)) ///
	ysize(3) xsize(5) ///
	legend(off) ///
	ytitle("First-Differenced Change in Indexed" " Spending Relative to DoW FEs (%)")

	oi_graph_export "${root}/results/Policy/Stimulus scatterplot for April round window Q1`graph_title'", type(${fig_type})

	* Black and white version for QJE
	if "`depvar'" == "first_diff_notrend_recenter" {
		tw ///
		(scatter `depvar' date, mcolor(gs0)) ///
		, ///
		xlab(`=mdy(3, 18, 2020)' `""Mar 18" "2020""' `=mdy(3, 25, 2020)' "Mar 25" ///
			`=mdy(4, 1, 2020)' "Apr 1" `=mdy(4, 8, 2020)' "Apr 8" `=mdy(4, 15, 2020)' "Apr 15" ///
			`=mdy(4, 22, 2020)' "Apr 22" `=mdy(4, 29, 2020)' "Apr 29" `=mdy(5, 6, 2020)' "May 6" ) ///
		xline(`=mdy(4,14,2020)', lcolor(gs8) lpattern(dash)) ///
		xline(`=mdy(4,15,2020)', lcolor(gs8)) ///
		text(57 `=mdy(4,14,2020) - 1' "Apr" "14", color(gs8) size(medsmall))  ///
		text(57 `=mdy(4,15,2020) + 1' "Apr" "15", color(gs8) size(medsmall)) ///
		title(" ", size(tiny)) ///
		yline(0, lcolor(gs8) lpattern(dash)) ///
		xtitle("") ///
		ylab(-20 "-20%" 0 "0%" 20 "+20%" 40 "+40%", nogrid) ///
		yscale(range(-20 50)) ///
		ysize(3) xsize(5) ///
		legend(off) ///
		ytitle("First-Differenced Change in Indexed" " Spending Relative to DoW FEs (%)")

		graph export "${root}/results/QJE_Figures_BlackAndWhite/Figure_7a.svg", replace
		project, creates("${root}/results/QJE_Figures_BlackAndWhite/Figure_7a.svg")
	}

	restore

	* April, Q4
	preserve

	keep if round == "april" & income_q == 4 & analysis_window == 1
	gisid date

	tw ///
	(scatter `depvar' date, mcolor(oi2) msymbol(T)) ///
	, ///
	xlab(`=mdy(3, 18, 2020)' `""Mar 18" "2020""' `=mdy(3, 25, 2020)' "Mar 25" ///
		 `=mdy(4, 1, 2020)' "Apr 1" `=mdy(4, 8, 2020)' "Apr 8" `=mdy(4, 15, 2020)' "Apr 15" ///
		 `=mdy(4, 22, 2020)' "Apr 22" `=mdy(4, 29, 2020)' "Apr 29" `=mdy(5, 6, 2020)' "May 6" ) ///
	xline(`=mdy(4,14,2020)', lcolor(gs8) lpattern(dash)) ///
	xline(`=mdy(4,15,2020)', lcolor(gs8)) ///
	text(57 `=mdy(4,14,2020) - 1' "Apr" "14", color(gs8) size(medsmall))  ///
	text(57 `=mdy(4,15,2020) + 1' "Apr" "15", color(gs8) size(medsmall)) ///
	title(" ", size(tiny)) ///
	yline(0, lcolor(gs8) lpattern(dash)) ///
	xtitle("") ///
	ylab(-20 "-20%" 0 "0%" 20 "+20%" 40 "+40%", nogrid) ///
	yscale(range(-20 50)) ///
	ysize(3) xsize(5) ///
	legend(off) ///
	ytitle("First-Differenced Change in Indexed" " Spending Relative to DoW FEs (%)")

	oi_graph_export "${root}/results/Policy/Stimulus scatterplot for April round window Q4`graph_title'", type(${fig_type})


	* Black and white version for QJE
	if "`depvar'" == "first_diff_notrend_recenter" {
		tw ///
		(scatter `depvar' date, mcolor(gs8) msymbol(T)) ///
		, ///
		xlab(`=mdy(3, 18, 2020)' `""Mar 18" "2020""' `=mdy(3, 25, 2020)' "Mar 25" ///
			`=mdy(4, 1, 2020)' "Apr 1" `=mdy(4, 8, 2020)' "Apr 8" `=mdy(4, 15, 2020)' "Apr 15" ///
			`=mdy(4, 22, 2020)' "Apr 22" `=mdy(4, 29, 2020)' "Apr 29" `=mdy(5, 6, 2020)' "May 6" ) ///
		xline(`=mdy(4,14,2020)', lcolor(gs8) lpattern(dash)) ///
		xline(`=mdy(4,15,2020)', lcolor(gs8)) ///
		text(57 `=mdy(4,14,2020) - 1' "Apr" "14", color(gs8) size(medsmall))  ///
		text(57 `=mdy(4,15,2020) + 1' "Apr" "15", color(gs8) size(medsmall)) ///
		title(" ", size(tiny)) ///
		yline(0, lcolor(gs8) lpattern(dash)) ///
		xtitle("") ///
		ylab(-20 "-20%" 0 "0%" 20 "+20%" 40 "+40%", nogrid) ///
		yscale(range(-20 50)) ///
		ysize(3) xsize(5) ///
		legend(off) ///
		ytitle("First-Differenced Change in Indexed" " Spending Relative to DoW FEs (%)")

		graph export "${root}/results/QJE_Figures_BlackAndWhite/Figure_7b.svg", replace
		project, creates("${root}/results/QJE_Figures_BlackAndWhite/Figure_7b.svg")
	}

	restore

	* April, Q1 & Q4
	preserve

	keep if round == "april" & inlist(income_q, 1, 4) & analysis_window == 1
	gisid income_q date

	tw ///
	(scatter `depvar' date if income_q == 1, mcolor(oi1) ) ///
	(scatter `depvar' date if income_q == 4, mcolor(oi2) msymbol(T)) ///
	, ///
	xlab(`=mdy(3, 18, 2020)' `""Mar 18" "2020""' `=mdy(3, 25, 2020)' "Mar 25" ///
		 `=mdy(4, 1, 2020)' "Apr 1" `=mdy(4, 8, 2020)' "Apr 8" `=mdy(4, 15, 2020)' "Apr 15" ///
		 `=mdy(4, 22, 2020)' "Apr 22" `=mdy(4, 29, 2020)' "Apr 29" `=mdy(5, 6, 2020)' "May 6" ) ///
	xline(`=mdy(4,14,2020)', lcolor(gs8) lpattern(dash)) ///
	xline(`=mdy(4,15,2020)', lcolor(gs8)) ///
	text(57 `=mdy(4,14,2020) - 1' "Apr" "14", color(gs8) size(medsmall))  ///
	text(57 `=mdy(4,15,2020) + 1' "Apr" "15", color(gs8) size(medsmall)) ///
	title(" ", size(tiny)) ///
	yline(0, lcolor(gs8) lpattern(dash)) ///
	xtitle("") ///
	ylab(-20 "-20%" 0 "0%" 20 "+20%" 40 "+40%", nogrid) ///
	yscale(range(-20 50)) ///
	ysize(3) xsize(5) ///
	legend(order(1 "Bottom Income Quartile" 2 "Top Income Quartile") ring(0) pos(2) cols(1)) ///
	ytitle("First-Differenced Change in Indexed" " Spending Relative to DoW FEs (%)")

	oi_graph_export "${root}/results/Policy/Stimulus scatterplot for April round Q1 and 4 pre line`graph_title'", type(${fig_type})

	restore

	* January, Q1 & Q4
	preserve

	keep if round == "january" & inlist(income_q, 1, 4) & analysis_window == 1
	gisid income_q date

	* Rescale x-axis for presentation to omit the holiday period
	gen graph_date = date - mdy(1, 4, 2021) if date >= mdy(1, 4, 2021) // this should create a var that's numbered in days relative to the stimulus
	replace graph_date = date - mdy(12, 15, 2020) if date <= mdy(12, 14, 2020) // this will make the variable e.g. -1 on December 14

	tw ///
	(scatter `depvar' graph_date if income_q == 1, mcolor(oi1)) ///
	(scatter `depvar' graph_date if income_q == 4, mcolor(oi2) msymbol(T)) ///
	, ///
	xlab(-11 `""Dec 4" "2020""' -7 "Dec 8" -3 "Dec 12" 0 `""Jan 4" "2021""' 4 "Jan 8" 8 "Jan 12" 12 "Jan 16") ///
	yline(0, lcolor(gs8) lpattern(dash)) ///
	xline(0, lcolor(gs8)) ///
	text(57 0 "Jan" "4", color(gs8) size(medsmall)) ///
	title(" ", size(tiny)) ///
	xtitle("") ///
	ylab(-20 "-20%" 0 "0%" 20 "+20%" 40 "+40%", nogrid) ///
	yscale(range(-20 50)) ///
	ysize(3) xsize(5) ///
	text(-22 -1.5 "//", color(gs8) box bcolor(white)) ///
	legend(order(1 "Bottom Income Quartile" 2 "Top Income Quartile") ring(0) pos(1) cols(1)) ///
	ytitle("First-Differenced Change in Indexed" " Spending Relative to DoW FEs (%)")

	oi_graph_export "${root}/results/Policy/Stimulus scatterplot for Jan round Q1 and 4`graph_title'", type(${fig_type})

	if "`depvar'" == "first_diff_notrend_recenter" {
		tw ///
		(scatter `depvar' graph_date if income_q == 1, mcolor(gs0)) ///
		(scatter `depvar' graph_date if income_q == 4, mcolor(gs8) msymbol(T)) ///
		, ///
		xlab(-11 `""Dec 4" "2020""' -7 "Dec 8" -3 "Dec 12" 0 `""Jan 4" "2021""' 4 "Jan 8" 8 "Jan 12" 12 "Jan 16") ///
		yline(0, lcolor(gs8) lpattern(dash)) ///
		xline(0, lcolor(gs8)) ///
		text(57 0 "Jan" "4", color(gs8) size(medsmall)) ///
		title(" ", size(tiny)) ///
		xtitle("") ///
		ylab(-20 "-20%" 0 "0%" 20 "+20%" 40 "+40%", nogrid) ///
		yscale(range(-20 50)) ///
		ysize(3) xsize(5) ///
		text(-22 -1.5 "//", color(gs8) box bcolor(white)) ///
		legend(order(1 "Bottom Income Quartile" 2 "Top Income Quartile") ring(0) pos(1) cols(1)) ///
		ytitle("First-Differenced Change in Indexed" " Spending Relative to DoW FEs (%)")

		graph export "${root}/results/QJE_Figures_BlackAndWhite/Figure_7c.svg", replace
		project, creates("${root}/results/QJE_Figures_BlackAndWhite/Figure_7c.svg")
	}

	restore

	* March, Q1 & Q4
	preserve

	keep if round == "march" & inlist(income_q, 1, 4) & analysis_window == 1
	gisid income_q date

	tw ///
	(scatter `depvar' date if income_q == 1, mcolor(oi1) ) ///
	(scatter `depvar' date if income_q == 4, mcolor(oi2) msymbol(T)) ///
	, ///
	xlab(`=mdy(2, 17, 2021)' `""Feb 17" "2021""' `=mdy(2, 24, 2021)' "Feb 24" `=mdy(3, 3, 2021)' "Mar 3" ///
		 `=mdy(3, 10, 2021)' "Mar 10" `=mdy(3, 17, 2021)' "Mar 17" `=mdy(3, 24, 2021)' "Mar 24" `=mdy(3, 31, 2021)' "Mar 31" ///
		 `=mdy(4, 7, 2021)' "Apr 7") ///
	xline(`=mdy(3, 13, 2021)', lcolor(gs8) lpattern(dash)) ///
	xline(`=mdy(3, 17, 2021)', lcolor(gs8)) ///
	text(57 `=mdy(3, 13, 2021)' "Mar" "13", color(gs8) size(medsmall))  ///
	text(57 `=mdy(3, 17, 2021)' "Mar" "17", color(gs8) size(medsmall)) ///
	title(" ", size(tiny)) ///
	yline(0, lcolor(gs8) lpattern(dash)) ///
	xtitle("") ///
	ylab(-20 "-20%" 0 "0%" 20 "+20%" 40 "+40%", nogrid) ///
	yscale(range(-20 50)) ///
	ysize(3) xsize(5) ///
	legend(order(1 "Bottom Income Quartile" 2 "Top Income Quartile") ring(0) pos(2) cols(1)) ///
	ytitle("First-Differenced Change in Indexed" " Spending Relative to DoW FEs (%)")

	oi_graph_export "${root}/results/Policy/Stimulus scatterplot for March round Q1 and 4 pre line`graph_title'", type(${fig_type})

	if "`depvar'" == "first_diff_notrend_recenter" {
		tw ///
		(scatter `depvar' date if income_q == 1, mcolor(gs0) ) ///
		(scatter `depvar' date if income_q == 4, mcolor(gs8) msymbol(T)) ///
		, ///
		xlab(`=mdy(2, 17, 2021)' `""Feb 17" "2021""' `=mdy(2, 24, 2021)' "Feb 24" `=mdy(3, 3, 2021)' "Mar 3" ///
			`=mdy(3, 10, 2021)' "Mar 10" `=mdy(3, 17, 2021)' "Mar 17" `=mdy(3, 24, 2021)' "Mar 24" `=mdy(3, 31, 2021)' "Mar 31" ///
			`=mdy(4, 7, 2021)' "Apr 7") ///
		xline(`=mdy(3, 13, 2021)', lcolor(gs8) lpattern(dash)) ///
		xline(`=mdy(3, 17, 2021)', lcolor(gs8)) ///
		text(57 `=mdy(3, 13, 2021)' "Mar" "13", color(gs8) size(medsmall))  ///
		text(57 `=mdy(3, 17, 2021)' "Mar" "17", color(gs8) size(medsmall)) ///
		title(" ", size(tiny)) ///
		yline(0, lcolor(gs8) lpattern(dash)) ///
		xtitle("") ///
		ylab(-20 "-20%" 0 "0%" 20 "+20%" 40 "+40%", nogrid) ///
		yscale(range(-20 50)) ///
		ysize(3) xsize(5) ///
		legend(order(1 "Bottom Income Quartile" 2 "Top Income Quartile") ring(0) pos(2) cols(1)) ///
		ytitle("First-Differenced Change in Indexed" " Spending Relative to DoW FEs (%)")

		graph export "${root}/results/QJE_Figures_BlackAndWhite/Figure_7d.svg", replace
		project, creates("${root}/results/QJE_Figures_BlackAndWhite/Figure_7d.svg")
	}

	restore
}
